package com.yowosoft.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yowosoft.model.LinkMessageType;
import com.yowosoft.model.TextMessageType;
import com.yowosoft.util.CheckUtils;
import com.yowosoft.util.MessageUtils;

@Controller
@RequestMapping("/weixin")
public class WXOneController {
	@RequestMapping(value = "/link.do", method = RequestMethod.GET)
	public void welcome(LinkMessageType messageType, HttpServletResponse response) throws IOException {
		/*
		 * 获取参数方法1
		 */
		// MessageType messageType = new MessageType();
		// messageType.setEchostr(request.getParameter("echostr"));
		// messageType.setNonce(request.getParameter("nonce"));
		// messageType.setSignature(request.getParameter("signature"));
		// messageType.setTimestamp(request.getParameter("timestamp"));
		// System.out.println(messageType.toString());

		PrintWriter out = response.getWriter();
		System.err.println("signature:" + messageType.getSignature() + "echostr:" + messageType.getEchostr());
		if (CheckUtils.checkSignature(messageType)) {
			out.print(messageType.getEchostr());
		}
	}

	@RequestMapping(value = "/link.do", method = RequestMethod.POST)
	public void welcomeTwo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HashMap<String, String> map;
		String message = null;
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		try {
			map = MessageUtils.xmlToMap(request);
			TextMessageType textMessageType = new TextMessageType();
			textMessageType.setFromUserName(map.get("FromUserName"));
			textMessageType.setToUserName(map.get("ToUserName"));
			textMessageType.setMsgType(map.get("MsgType"));
			textMessageType.setContent(map.get("Content"));
			if (MessageUtils.MESSAGE_TEXT.equals(textMessageType.getMsgType())) {
				// 测试1
				// TextMessageType responseText = new TextMessageType();
				// responseText.setFromUserName(textMessageType.getToUserName());
				// responseText.setToUserName(textMessageType.getFromUserName());
				// responseText.setMsgType(MessageUtils.MESSAGE_TEXT);
				// responseText.setCreateTime(new Date().getTime());
				// responseText.setContent("您发送的消息是：" +
				// textMessageType.getContent());
				// message = MessageUtils.textMessageToXml(responseText);
				// System.out.println(message);
				//测试2
				if ("1".equals(textMessageType.getContent())) {
					message = MessageUtils.initText(textMessageType.getToUserName(), textMessageType.getFromUserName(),
							MessageUtils.firstMenu());
				} else if ("2".equals(textMessageType.getContent())) {
					message = MessageUtils.initNewsMessage(textMessageType.getToUserName(),
							textMessageType.getFromUserName());
				} else if ("3".equals(textMessageType.getContent())) {
					message = MessageUtils.initImageMessage(textMessageType.getToUserName(),
							textMessageType.getFromUserName());
				} else if ("4".equals(textMessageType.getContent())) {
					message = MessageUtils.initMusicMessage(textMessageType.getToUserName(),
							textMessageType.getFromUserName());
				} else if ("?".equals(textMessageType.getContent()) || "？".equals(textMessageType.getContent())) {
					message = MessageUtils.initText(textMessageType.getToUserName(), textMessageType.getFromUserName(),
							MessageUtils.menuText());
				}
				//图灵机器人接入测试：
				
				

			} else if (MessageUtils.MESSAGE_EVENT.equals(textMessageType.getMsgType())) {
				String eventType = map.get("Event");
				if (MessageUtils.MESSAGE_SUBSCRIBE.equals(eventType)) {
					message = MessageUtils.initText(textMessageType.getToUserName(), textMessageType.getFromUserName(),
							MessageUtils.menuText());
				}
			}
			out.print(message);
		} catch (DocumentException e) {
			e.printStackTrace();
		} finally {
			out.close();
		}
	}
}
